package oleg; 

/**
 * Copyright 2008,2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.pim.*;

import oleg.cfg.Config;
import oleg.locale.LocaleManager;
import oleg.log.Logger;

public class ConfigureView
{
  private MainSearch mainSearch;

  private Form configForm = new Form("Configure");
  private ChoiceGroup langGroup = null;
  private ChoiceGroup pimListGroup = null;
  private Display display;
  private Displayable prevScreen;

  private Command saveCommand = new Command("Save", Command.OK, 2);
  private Command cancelCommand = new Command("Back", Command.CANCEL, 2);

  String[] allLocales;
  String[] allPimLists;

  public ConfigureView(MainSearch mainSearch) {
    this.mainSearch = mainSearch;
    display = Display.getDisplay(mainSearch);

    updateInterface();

    configForm.setCommandListener(new ViewCommandListener());

    initForm();
  }

  public void updateInterface() {
//     phonesList.removeCommand(exitCommand);
    configForm.removeCommand(saveCommand);
    configForm.removeCommand(cancelCommand);


    LocaleManager localeMgr = LocaleManager.instance();

    saveCommand = new Command(localeMgr.getProperty(C.SAVE), Command.OK, 2);
    cancelCommand = new Command(localeMgr.getProperty(C.BACK), Command.CANCEL, 2);
    configForm.addCommand(saveCommand);
    configForm.addCommand(cancelCommand);
  }

  private void initForm() {
    initLangGroup();
    initPimLists();

    configForm.append(langGroup);
    configForm.append(pimListGroup);
  }

  private void initPimLists() {
    LocaleManager localeMgr = LocaleManager.instance();
    PIM pim = PIM.getInstance();
    allPimLists = pim.listPIMLists(PIM.CONTACT_LIST);
    pimListGroup = new ChoiceGroup(localeMgr.getProperty(C.PIM_LIST),
                                   Choice.MULTIPLE,
                                   allPimLists,
                                   null);
    String[] curPimLists = Config.instance().getPimLists();
    if (curPimLists == null || curPimLists.length == 0) {
      return;
    }

    for (int i = 0; i < allPimLists.length; i++) {
      for (int j = 0; j < curPimLists.length; j++) {
        if (curPimLists[j].equals(allPimLists[i])) {
          pimListGroup.setSelectedIndex(i, true);
        }
      }
    }

  }

  private void initLangGroup() {
    LocaleManager localeMgr = LocaleManager.instance();
    allLocales = localeMgr.getAllLocaleNames();
    String[] humanAllLocales = new String[allLocales.length];
    for (int i = 0; i < allLocales.length; i++) {
      humanAllLocales[i] = localeMgr.getProperty(allLocales[i], C.HUMAN_LOCALE);
    }
    langGroup = new ChoiceGroup("Interface Language",
                                Choice.EXCLUSIVE,
                                humanAllLocales,
                                null);
    String curLocale = Config.instance().getInterfaceLang();
    for (int i = 0; i < allLocales.length; i++) {
      if (curLocale.equals(allLocales[i])) {
        langGroup.setSelectedIndex(i, true);
        break;
      }
    }

  }

  public void showView() {
    prevScreen = display.getCurrent();
    display.setCurrent(configForm);
  }


  private class ViewCommandListener implements CommandListener
  {
    public void commandAction(Command c, Displayable s) {
      try {
        //       if (c == exitCommand) {
        //         mainSearch.exitApplication();
        //         return;
        //       }

        if (c == saveCommand) {
          // Save interface
          String lang = allLocales[langGroup.getSelectedIndex()];
          Config.instance().setInterfaceLang(lang);
          mainSearch.getMainForm().updateInterface();

          // Save PIM device
          // First find number of selected items
          int selCount = 0;
          for (int i = 0; i < allPimLists.length; i++) {
            if (pimListGroup.isSelected(i)) {
              selCount++;
            }
          }
          String[] devices = new String[selCount];
          selCount = 0;
          for (int i = 0; i < allPimLists.length; i++) {
            if (pimListGroup.isSelected(i)) {
              devices[selCount++] = allPimLists[i];
            }
          }
          Config.instance().setPimLists(devices);
          mainSearch.getMainForm().updatePimLists();

          display.setCurrent(prevScreen);
          return;
        }

        if (c == cancelCommand) {
          display.setCurrent(prevScreen);
          return;
        }
      } catch (Exception ex) {
        Logger.log("ConfigureView: " + ex);
      }
    }
  }

}
